const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
    entry:{
        index:path.join(__dirname,'./src/js/index.js'),
        cart:path.join(__dirname,'./src/js/cart.js'),
        vender:[path.join(__dirname,'./src/js/common.js'),'jquery']
    },
    output:{
        path:path.join(__dirname,'./dist'),
        filename:'js/[name].js',
        //publicPath:'/public'
    },
    resolve:{
        extensions:['.js']
    },
    module:{
        rules:[
            {
                test:/\.css$/,
                exclude:[
                    path.join(__dirname,'./node_modules/')
                ],
                use:['style-loader','css-loader'],
            },
            {
                test:/\.js$/,
                use: 'babel-loader',
                exclude:[
                    path.join(__dirname,'./node_modules')
                ]
            },
        ]
    },
    // 压缩代码
    optimization: {
        minimizer: [
          new UglifyJsPlugin({
            test: /\.js(\?.*)?$/i,
          }),
        ],
      },
    plugins:[
        new HtmlWebpackPlugin({
            filename:'index.html',
            template:path.join(__dirname,'./src/index.html'),
            chunks:['index','vender'],
            minify:{
                removeComments:true,
                removeTagWhitespace:true
            }
        }),
        new HtmlWebpackPlugin({
            filename:'cart.html',
            template:path.join(__dirname,'./src/cart.html'),
            chunks:['cart','vender'],
            minify:{
                removeComments:true,
                removeTagWhitespace:true
            }
        }),
        new CleanWebpackPlugin(),
        new webpack.ProvidePlugin({
            $:'jquery',
            jQuery:'jquery',
            'window.jQuery':'jquery'
        }),
        // 分离公共部分
        // 不太了解
        new webpack.optimize.SplitChunksPlugin({
            chunks: "all",
            minSize: 20000,
            minChunks: 1,
            maxAsyncRequests: 5,
            maxInitialRequests: 3,
            name: true
        }),
        // 压缩代码
    ],
    devtool:'#source-map'
};